﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test1
{
    public partial class Form4 : Form
    {
        private int count = 1;
        TreeView TV = new TreeView();

        public Form4()
        {
            InitializeComponent();
            //DragDrop += Form1_DragDrop;
            //DragEnter += Form1_DragEnter;
            //Load += Form3_Load;

            TV.Parent = this;
            TV.Dock = DockStyle.Fill;
            TV.ItemHeight = 30;

            TreeNode Root = TV.Nodes.Add("根");

            AddDeleteButtonChildNode(Root, "1");
            AddDeleteButtonChildNode(Root, "2");
            AddDeleteButtonChildNode(Root, "3");

            TV.AfterExpand += new TreeViewEventHandler(TV_AfterExpand);
            TV.AfterCollapse += new TreeViewEventHandler(TV_AfterCollapse);
        }

        //private void Form3_Load(object sender, EventArgs e)
        //{
        //    this.AllowDrop = true;
        //    //窗体自身支持接受拖拽来的控件
        //}

        void TV_AfterCollapse(object sender, TreeViewEventArgs e)
        {
            ChangeDeleteButtonVisible(e.Node, false);
        }

        void TV_AfterExpand(object sender, TreeViewEventArgs e)
        {
            ChangeDeleteButtonVisible(e.Node, true);
        }

        void ChangeDeleteButtonVisible(TreeNode ParentNode, bool Visible)
        {
            foreach (TreeNode ChildNode in ParentNode.Nodes)
            {
                Button B = (Button)ChildNode.Tag;
                B.Location = ChildNode.Bounds.Location;
                B.Left += ChildNode.Bounds.Width;
                B.Visible = Visible;
            }
        }

        private Button B;
        void AddDeleteButtonChildNode(TreeNode ParentNode, String Text)
        {
            TreeNode Node = ParentNode.Nodes.Add(Text);

            B = new Button();
            B.Parent = TV;
            B.Text = "删除";
            B.Visible = false;
            B.Click += new EventHandler(B_Click);
            //B.MouseDown += new MouseEventHandler(B_MouseDown);

            B.Tag = Node;
            Node.Tag = B;
        }

        void B_Click(object sender, EventArgs e)
        {
            Button B = (Button)sender;
            TreeNode Node = (TreeNode)B.Tag;
            TreeNode ParentNode = Node.Parent;

            B.Dispose();
            Node.Remove();

            ChangeDeleteButtonVisible(ParentNode, true); // 刷新重新定位按钮
        }

        ////void B_MouseDown(object sender, MouseEventArgs e)
        ////{

        ////    //左键的话，标志位为true（表示拖拽开始）
        ////    if ((e.Button == System.Windows.Forms.MouseButtons.Left))
        ////    {
        ////        B.DoDragDrop(B, DragDropEffects.Copy | DragDropEffects.Move);
        ////        //形成拖拽效果，移动+拷贝的组合效果
        ////    }
        ////}

        ////private void Form1_DragDrop(object sender, DragEventArgs e)
        ////{
        ////    //拖放完毕之后，自动生成新控件
        ////    Button btn = new Button();
        ////    btn.Size = B.Size;
        ////    btn.Location = this.PointToClient(new Point(e.X, e.Y));
        ////    //用这个方法计算出客户端容器界面的X，Y坐标。否则直接使用X，Y是屏幕坐标
        ////    this.Controls.Add(btn);
        ////    btn.Text = "按钮" + count.ToString();
        ////    count = count + 1;
        ////}

        ////private void Form1_DragEnter(object sender, DragEventArgs e)
        ////{
        ////    //当Button被拖拽到WinForm上时候，鼠标效果出现
        ////    if ((e.Data.GetDataPresent(typeof(Button))))
        ////    {
        ////        e.Effect = DragDropEffects.Copy;
        ////    }
        ////}
    }
}
